package edu.cmu.ece.ds.droidchat.controller;

import java.util.ArrayList;

import android.content.Context;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnLongClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import edu.cmu.ece.ds.droidchat.R;
import edu.cmu.ece.ds.droidchat.Toolbox;
import edu.cmu.ece.ds.droidchat.model.ChatMessage;
import edu.cmu.ece.ds.droidchat.model.UserInfo;
import edu.cmu.ece.ds.droidchat.widget.ActionItem;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList;
import edu.cmu.ece.ds.droidchat.widget.QuickActionList.OnListClickListener;

public class ChatSessionMessageBaseAdapter extends BaseAdapter {
    private ArrayList<ChatMessage> messageArray;
    private LayoutInflater mInflater;

    public ChatSessionMessageBaseAdapter(Context context,
            ArrayList<ChatMessage> messageArray) {
        this.messageArray = messageArray;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return messageArray.size();
    }

    @Override
    public Object getItem(int position) {
        return messageArray.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        ChatMessage chatMessage = messageArray.get(position);
        return chatMessage.getUserinfo().getUsername()
                .equals(Toolbox.getMyInfo().getUsername()) ? 0 : 1;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        final ChatMessage chatMessage = messageArray.get(position);
        final UserInfo userInfo = chatMessage.getUserinfo();

        if (convertView == null) {
            if (userInfo.getUsername()
                    .equals(Toolbox.getMyInfo().getUsername())) {
                convertView = mInflater.inflate(R.layout.chat_message_row_me,
                        null);
            } else {
                convertView = mInflater.inflate(
                        R.layout.chat_message_row_other, null);
            }
            viewHolder = new ViewHolder();
            viewHolder.message = (TextView) convertView
                    .findViewById(R.id.message_text_view);

            // Choose a chat box
            if (userInfo.getUsername()
                    .equals(Toolbox.getMyInfo().getUsername())) {
                // Well... what color do I like? Can be random.
                viewHolder.message
                        .setBackgroundResource(R.drawable.chat_box_lime_right);

            } else {
                // Well... what color do you like? Can be random.
                viewHolder.message
                        .setBackgroundResource(R.drawable.chat_box_aqua_left);
            }

            viewHolder.sender = (ImageView) convertView
                    .findViewById(R.id.message_sender);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.message.setText(Html.fromHtml("<b>" + userInfo.getNickName()
                + ": " + "</b>" + chatMessage.getMessageContent()));
        viewHolder.sender.setImageDrawable(userInfo.getProfilePicture());

        final View cView = convertView;
        viewHolder.message
                .setOnLongClickListener(new MessageOnLongClickListener(
                        chatMessage));
        viewHolder.sender.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO the user popup window
                Toast.makeText(cView.getContext(), userInfo.getUsername(),
                        Toast.LENGTH_SHORT).show();
            }

        });
        return convertView;
    }

    static class ViewHolder {
        public TextView message;
        public ImageView sender;
    }

    private class MessageOnLongClickListener implements OnLongClickListener {
        private ChatMessage message;

        public MessageOnLongClickListener(ChatMessage cm) {
            this.message = cm;
        }

        @Override
        public boolean onLongClick(View v) {
            // TODO menu to copy
            Toast.makeText(v.getContext(), ((TextView) v).getText(),
                    Toast.LENGTH_SHORT).show();
            v.getRootView().setOnTouchListener(new OnTouchListener(){

                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        Log.d("onTouch","action??");
                        if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
                            Log.d("onTouch","action outside");
                            return true;
                        }

                        return false;
                    }

            });

            final QuickActionList l = new QuickActionList(v,LinearLayout.VERTICAL);

            l.addActionItem(new ActionItem(v.getContext(), 0,
                    R.string.location_cmu,LinearLayout.HORIZONTAL));
            l.setOnListClickListener(new OnListClickListener(){

                @Override
                public void onQuickActionListClicked(View contentView,
                        int position) {
                    // TODO Auto-generated method stub


                }});

            if (message.getUserinfo().getUsername().equals(Toolbox.getMyInfo().getUsername())) {
                v.setBackgroundResource(R.drawable.chat_box_aqua_right);
                v.setOnFocusChangeListener(new OnFocusChangeListener(){

                    @Override
                    public void onFocusChange(View v, boolean hasFocus) {
                        Log.d("onFocusChange","v="+v+",hasFocus="+hasFocus);
                    }});
            }
            return true;
        }
    }

}
